//删除
function del(obj,id,url){
    layer.confirm('确认要删除吗？',function(index){
        $.ajax({
            type:'post',
            data:{'id':id},
            url:url,
            dataType:'json',
            async:false,
            success:function(e){
                if(e.code === 1){
                    $(obj).parents("tr").remove();
                    layer.msg('删除成功',{icon:1,time:1000});
                }else{
                    layer.msg(e.msg,{icon:2,time:1000});
                }
            }
        });
    });
}

//获取菜单的帮助信息
function menu_help(id,url) {
    if(id === ''){
        layer.msg('没有找到菜单id');
        return;
    }
    $.ajax({
        type:'post',
        data:{"id":id},
        url:url,
        dataType:'json',
        async:false,
        success:function(e){
            if(e.code === 1){
                if(e.data.info === ''){
                    layer.msg('暂无帮助信息');
                }else{
                    layer.open({
                        type: 1,
                        title:'帮助信息',
                        skin: 'yourclass',
                        area: ['550px', '350px'], //宽高
                        content: e.data.info
                    });
                }
            }else{
                layer.msg(e.msg,{icon:2,time:1000});
            }
        }
    });
}

//更改状态
function set_status(obj,id,url) {
    var status = $(obj).data('status');
    var s = status === 1 ? 2 : 1;
    var field = $(obj).data('field');
    layer.confirm('确认要更改吗？',function(index){
        $.ajax({
            type:'post',
            data:field+'='+id+'&status='+s,
            url:url,
            dataType:'json',
            async:false,
            success:function (e) {
                if(e.code === 1){
                    if(status === 1){
                        $(obj).data('status',0);
                        $(obj).attr('title','点击更改为启用');
                        $(obj).addClass('layui-btn-danger');
                        $(obj).removeClass('layui-btn-normal');
                        $(obj).text('禁用');
                    }else{
                        $(obj).data('status',1);
                        $(obj).attr('title','点击更改为禁用');
                        $(obj).removeClass('layui-btn-danger');
                        $(obj).addClass('layui-btn-normal');
                        $(obj).text('启用');
                    }
                    layer.msg(e.msg,{icon:1,time:1000});
                }else{
                    layer.msg(e.msg,{icon:2,time:1000});
                }
            }
        });
    });
}

//请求确认接口,只需要返回是否成功
function request_result(url,title){
    if(typeof(title) == 'undefined'){
        title = '确认吗？';
    }
    layer.confirm(title,function(index){
        $.ajax({
            type:'post',
            url:url,
            dataType:'json',
            async:false,
            success:function(e){
                if(e.code === 1){
                    layer.msg(e.msg,{icon:1,time:1000});
                }else{
                    layer.msg(e.msg,{icon:2,time:1000});
                }
            }
        });
    });
}

//请求接口,只需要返回是否成功 - 无需确认的请求
function request_result_true(url){
    $.ajax({
        type:'post',
        url:url,
        dataType:'json',
        async:false,
        success:function(e){
            if(e.code === 1){
                layer.msg(e.msg,{icon:1,time:1000});
            }else{
                layer.msg(e.msg,{icon:2,time:1000});
            }
        }
    });
}

/*
* 导出excel,在当前的url上添加export=1的参数
* */
function export_excel() {
    var url = location.href;
    if(url.indexOf('?') === -1){
        url += '?export=1';
    }else{
        url += '&export=1';
    }
    window.open(url,'_blank');
}

/*
* 文件上传
* var file 文件
* var field input的字段名称
* var save 保存的文件名称
* var url 上传的url地址
* var id 对象的id
* return 上传的结果
* */
function upload(file,field,url,id,save) {
    var xhr = new XMLHttpRequest();
    var data = new FormData();
    if(typeof(save) != 'undefined'){
        data.append('field',save);
    }
    data.append('field',field);
    data.append('file',file);
    xhr.open('post',url,false);
    xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
    var r = '';
    xhr.onreadystatechange = function() {
        if (xhr.readyState === 4 && (xhr.status === 200 || xhr.status === 304)) {
            var data = JSON.parse(xhr.responseText);
            if(data.code === 1){
                r = data.data;
                layer.msg('上传成功');
            }else{
                r = '';
                layer.msg(data.msg);
            }
        }else{
            layer.msg('系统错误,上传失败');
        }
    };
    xhr.send(data);
    return r;
}

//设置跳页
$(function () {
    $('.set_page').on('click',function () {
        var max_page = $(this).attr('datapage');
        var page_url = $(this).attr('page_url');
        var index = layer.prompt({
            title : '请输入要跳转的页码'
        },function(val, index){
            var p = parseInt(val);
            if(!Number.isInteger(p)){
                layer.msg('请输入正确的页码');
            }else{
                if(p > max_page){
                    layer.msg('输入数不能大于最大页数');
                }else if (p < 1){
                    layer.msg('输入页码不能小于1');
                }else{
                    document.location.href = page_url.replace('set_page',p);
                }
            }
            layer.close(index);
        });
    });
});